from django.contrib import admin
from music.models import Artist,Album,Song
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
# Register your models here.


@admin.register(Artist)
class ArtistAdmin(admin.ModelAdmin):
    list_display = ('name','image_data')
    readonly_fields = ('image_data',)

    def image_data(self,obj):
        if obj.pic_url:
            return mark_safe(u'<img src="%s" width="100px"/>' % (obj.pic_url.url))
        else:
            return '(no image)'

    image_data.short_description = _('头像')


@admin.register(Album)
class AlbumAdmin(admin.ModelAdmin):
    list_display = ('name','image_data')
    readonly_fields = ('image_data',)
    search_fields = ['name']
    list_filter = ['artist__name']

    def image_data(self,obj):
        if obj.pic_url:
            return mark_safe(u'<img src="%s" width="100px"/>' % (obj.pic_url.url))
        else:
            return '(no image)'

    image_data.short_description = _('头像')


@admin.register(Song)
class SongAdmin(admin.ModelAdmin):
    search_fields = ['name','alias']
    list_display = ['name','alias','album','created_at','updated_at']

